﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using testlan2.BLL;
using testlan2.DAL;

namespace testlan2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        xulyBLL BLL = new xulyBLL();
        private void btnTim_Click(object sender, EventArgs e)
        {
            try
            {
                HOCPHAN hp = BLL.getMA(txtMaHP.Text);
                txtMaHP.Text = hp.MaHP;
                txtTenHP.Text = hp.TenHP;
                txtSoTC.Text = hp.SoTC.ToString();
                txtSoTLT.Text = hp.SoTietLt.ToString();
                txtSoTTH.Text = hp.SoTietTh.ToString();

            }
            catch
            {
                MessageBox.Show("khong tim thấy");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadForm();
        }
        public void loadForm()
        {
            dataGridView1.DataSource = BLL.getTable();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                HOCPHAN hp = new HOCPHAN();
                hp.MaHP = txtMaHP.Text;
                hp.TenHP = txtTenHP.Text;
                hp.SoTC = int.Parse(txtSoTC.Text);
                hp.SoTietLt=int.Parse(txtSoTLT.Text);
                hp.SoTietTh = int.Parse(txtSoTTH.Text);

                BLL.add(hp);
                loadForm();
            }
            catch
            {
                MessageBox.Show("Khong them duoc");
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                HOCPHAN hp = BLL.getMA(txtMaHP.Text);
                hp.MaHP = txtMaHP.Text;
                hp.TenHP = txtTenHP.Text;
                hp.SoTC = int.Parse(txtSoTC.Text);
                hp.SoTietLt = int.Parse(txtSoTLT.Text);
                hp.SoTietTh = int.Parse(txtSoTTH.Text);

                BLL.updatehp();
                loadForm();
            }
            catch
            {
                MessageBox.Show("khong luu duoc");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                HOCPHAN hp = BLL.getMA(txtMaHP.Text);
                BLL.xoahp(hp);
                loadForm();
            }
            catch
            {
                MessageBox.Show("Khong xoa duoc");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong;
            dong = e.RowIndex;
            txtMaHP.Text = dataGridView1[0, dong].Value.ToString();
            txtTenHP.Text = dataGridView1[1, dong].Value.ToString();
            txtSoTC.Text = dataGridView1[2, dong].Value.ToString();
            txtSoTLT.Text = dataGridView1[3, dong].Value.ToString();
            txtSoTTH.Text = dataGridView1[4, dong].Value.ToString();
        }
    }
}
